package com.wxiao.exam.controller.manager;

import com.wxiao.exam.GradeService;
import com.wxiao.exam.PageBean;
import com.wxiao.exam.Result;
import com.wxiao.exam.pojo.GradeDO;
import com.wxiao.exam.pojo.vo.GradeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin/grade")
public class ManageGradeController {

    @Value("#{properties['grade.pageSize']}")
    int pageSize;//列表大小

    @Value("#{properties['grade.navigatePages']}")
    int size;//页码大小

    @Autowired
    GradeService gradeService;

    /**
     * @param query 查询条件    包括第几页 搜索条件
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String grade(@Validated GradeVO query, Model model) throws Exception {
        model.addAttribute("function", 2);
        query.setPageSize(pageSize);
        query.setSize(size);
        PageBean<GradeDO> pageBean = gradeService.listGrade(query);
        model.addAttribute("pageBean", pageBean);
        model.addAttribute("query", query);
        return "admin/grade_list";
    }


    @RequestMapping("/add/{grade}")
    @ResponseBody
    public Result add(@PathVariable(value = "grade") Integer grade) throws Exception {
        GradeVO gradeVO = new GradeVO();
        gradeVO.setGrade(grade);
        return gradeService.addGrade(gradeVO);
    }
}
